package com.example.stock.compone;

import cn.hutool.core.collection.CollectionUtil;
import com.example.stock.eo.StockStatEo;
import com.example.stock.service.StockStatService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: zorching
 * @Description
 * @Date: 2021/1/2 20:23
 * Copyright (c) 2019 北京新媒传信科技有限公司
 */
@Component
@EnableScheduling
@EnableAsync
public class EmaComponent {

    @Resource
    private StockStatService stockStatService;


    private int startStatistics = 0;
    public void startStatistics() {
        if (startStatistics > 0) {
            throw new RuntimeException("repeat!");
        }
        startStatistics++;
    }

    private List<StockStatEo> statEos = new ArrayList<>();
    private int m = 0;
    private int n = 0;
    public void recordTen(int mm, int nn, BigDecimal finalMoney, BigDecimal ext1){
        if (statEos.size() < 10) {
            statEos.add(new StockStatEo(mm, nn, finalMoney, Short.valueOf("1"),ext1));
        } else {
            StockStatEo statEo = statEos.stream().min(Comparator.comparing(StockStatEo::getMoney)).get();
            statEos.remove(statEo);
            statEos.add(new StockStatEo(mm, nn, finalMoney, Short.valueOf("1"),ext1));
        }
        m = mm;
        n = nn;
    }

//    @Scheduled(fixedRate = 30000)
    public void configureTasks() {
        stockStatService.remove(null);
        List<StockStatEo> statSave = new ArrayList<>(statEos);
        statSave.add(new StockStatEo(m, n, BigDecimal.ZERO, Short.valueOf("0"),null));
        stockStatService.saveBatch(statSave);
    }

    private List<StockStatEo> allStatEos = new ArrayList<>();
    public void recordAll(int mm, int nn, BigDecimal finalMoney, BigDecimal ext1){
        allStatEos.add(new StockStatEo(mm, nn, finalMoney, Short.valueOf("1"),ext1));
        if (allStatEos.size() == 100) {
            String uuid = UUID.randomUUID().toString();
            asyncSave(allStatEos,uuid);
            System.out.println("sync save batch: " + uuid);
            allStatEos = new ArrayList<>();
        }

    }
    public void finishRecordAll(){
        if (allStatEos.size() >0) {
            String uuid = UUID.randomUUID().toString();
            asyncSave(allStatEos,uuid);
            System.out.println("sync save batch: " + uuid);
            allStatEos = new ArrayList<>();
        }
    }

    @Async
    public void asyncSave(List<StockStatEo> allStatEos,String uuid) {
        stockStatService.saveBatch(allStatEos);
        System.out.println("finish save batch: " + uuid);
    }

    /**
     * Description: Java8 Stream分割list集合
     * @param list 集合数据
     * @param splitSize 几个分割一组
     * @return 集合分割后的集合
     */
    public static <T> List<List<T>> splitList(List<T> list, int splitSize) {
        //判断集合是否为空
        if (CollectionUtil.isEmpty(list))
            return Collections.emptyList();
        //计算分割后的大小
        int maxSize = (list.size() + splitSize - 1) / splitSize;
        //开始分割
        return Stream.iterate(0, n -> n + 1)
                .limit(maxSize)
                .parallel()
                .map(a -> list.parallelStream().skip(a * splitSize).limit(splitSize).collect(Collectors.toList()))
                .filter(b -> !b.isEmpty())
                .collect(Collectors.toList());
    }
}
